package com.adwin.addcoin.ui.dialog

import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.LayoutParams
import android.widget.Toast
import com.adwin.addcoin.base.BaseDialog
import com.adwin.addcoin.databinding.DialogOpinionFeedbackBinding
import com.adwin.addcoin.repository.SchedulerTransformer
import com.adwin.addcoin.repository.api.TestApi
import com.adwin.addcoin.repository.net.RetrofitClient
import com.adwin.addcoin.utils.installRepost
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import org.json.JSONObject

class OpinionFeedbackDialog(val mContext:Context):BaseDialog(mContext) {

    lateinit var binding:DialogOpinionFeedbackBinding

    override fun getGravity(): Int = Gravity.CENTER

    override fun getHeight(): Int = LayoutParams.WRAP_CONTENT

    override fun getWidth(): Int = LayoutParams.MATCH_PARENT

    override fun initLayout(): View {
         binding = DialogOpinionFeedbackBinding.inflate(LayoutInflater.from(mContext))
        return binding.root
    }

    override fun initData() {
        GlobalScope.launch(Dispatchers.IO) {
            context.installRepost("OpinionFeedbackDialogShow")
        }
        binding.tvSubmit.setOnClickListener {
            val message = binding.etOpinionFeedbackMessage.text.toString()
            if (TextUtils.isEmpty(message))return@setOnClickListener
            binding.etOpinionFeedbackMessage.setText("")
            GlobalScope.launch(Dispatchers.IO) {
                val jsonObject = JSONObject()
                jsonObject.put("remark",message)
                context.installRepost("FeedbackClick",jsonObject)
            }
            feedback(message)
        }
    }

    private fun feedback(message:String){
        val json = JSONObject()
        json.put("feedback",message)
        val requestBody: RequestBody = MultipartBody.create(MediaType.get("application/json;charset=utf-8"), json.toString())
        RetrofitClient.getInstance().getService(TestApi::class.java)
            .feedback(requestBody).compose(
            SchedulerTransformer()
        ).subscribe({
                    if (it.isSuccessful){
                        if (it.body()?.code == 200){
                            Toast.makeText(mContext,it.body()?.msg?:"",Toast.LENGTH_SHORT).show()
                            GlobalScope.launch(Dispatchers.IO) {
                                val jsonObject = JSONObject()
                                jsonObject.put("remark",message)
                                context.installRepost("FeedbackSuccess",jsonObject)
                            }
                            dismiss()
                        }else{
                            Toast.makeText(mContext,it.body()?.msg?:"",Toast.LENGTH_SHORT).show()
                        }
                    }else{
                        Toast.makeText(mContext,it.message()?:"",Toast.LENGTH_SHORT).show()
                    }
            },{
                Toast.makeText(mContext,it.message?:"",Toast.LENGTH_SHORT).show()
            })
    }
}